/**
 * 
 */
package com.mc.encryptDecrypt;

import java.util.Base64;

import org.apache.commons.codec.binary.Hex;

/**
 * @author w.weizhen
 *
 */
public class Test20221009 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			//十六进制
			String data = "000500050005000500050005000500050005000500050005000500050005000500050005000600050005000500050005000500050006000600050005";
			//十六进制转字符串
			System.out.println(hexStringToString(data));
			
			//示例编码和解码重现
			data="TEST";
			//字符串转十六进制
			System.out.println(strTo16(data));
			System.out.println(hexStringToString(strTo16(data)));
			//计算结果和示例相同
			//TEST
			//54455354
			
			//编码
			//十六进制
			data = "000500050005000500050005000500050005000500050005000500050005000500050005000600050005000500050005000500050006000600050005";
			byte[] textByte = Hex.decodeHex(data.toCharArray());
			String encodedText=Base64.getEncoder().encodeToString(textByte);
			System.out.println(encodedText);
			//解码
			data = "AAUABQAFAAUABQAFAAUABQAFAAUABQAFAAUABQAFAAUABQAFAAYABQAFAAUABQAFAAUABQAGAAYABQAF";
			byte[] x = Base64.getDecoder().decode(data);
			//十六进制
			data = Hex.encodeHexString(x);
			System.out.println(data);
			//十六进制转字符串
			System.out.println(hexStringToString(data));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 将16进制 ASCII转成字符串
	 *
	 * @param hexValue
	 * @return
	 */
	public static String asciiToString(String hexValue) {
		StringBuffer sbu = new StringBuffer();
		for (int i = 0; i < hexValue.length(); i += 2) {
			sbu.append((char) Integer.parseInt(hexValue.substring(i, i + 2), 16));
		}
		return sbu.toString();
	}
	
	/**
	 * 将字符串转成ASCII
	 *
	 * @param strValue
	 * @return
	 */
	public static String stringToAscii(String strValue) {
		StringBuffer sbu = new StringBuffer();
		char[] chars = strValue.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if (i != chars.length - 1) {
				sbu.append((int) chars[i]).append(",");
			} else {
				sbu.append((int) chars[i]);
			}
		}
		return sbu.toString();
	}
	
	/**
	 * 16进制转换成为string类型字符串
	 * 
	 * @param s
	 * @return
	 */
	public static String hexStringToString(String s) {
		if (s == null || s.equals("")) {
			return null;
		}
		s = s.replace(" ", "");
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "UTF-8");
			new String();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}
	
	/**
	 * 字符串转化成为16进制字符串
	 * 
	 * @param s
	 * @return
	 */
	public static String strTo16(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}
	
	/**
	 * 字符串转换unicode
	 */
	public static String string2Unicode(String string) {
		StringBuffer unicode = new StringBuffer();
		for (int i = 0; i < string.length(); i++) {
			// 取出每一个字符
			char c = string.charAt(i);
			// 转换为unicode
			unicode.append("\\u" + Integer.toHexString(c));
		}
		return unicode.toString();
	}
	
	/**
	 * unicode 转字符串
	 */
	public static String unicode2String(String unicode) {
		StringBuffer string = new StringBuffer();
		String[] hex = unicode.split("\\\\u");
		for (int i = 1; i < hex.length; i++) {
			// 转换出每一个代码点
			int data = Integer.parseInt(hex[i], 16);
			// 追加成string
			string.append((char) data);
		}
		return string.toString();
	}
	
	/**
	 * 字符串转换成为16进制(无需Unicode编码)
	 * 
	 * @param str
	 * @return
	 */
	public static String str2HexStr(String str) {
		char[] chars = "0123456789ABCDEF".toCharArray();
		StringBuilder sb = new StringBuilder("");
		byte[] bs = str.getBytes();
		int bit;
		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append(chars[bit]);
			bit = bs[i] & 0x0f;
			sb.append(chars[bit]);
			// sb.append(' ');
		}
		return sb.toString().trim();
	}
	
	/**
	 * 16进制直接转换成为字符串(无需Unicode解码)
	 * 
	 * @param hexStr
	 * @return
	 */
	public static String hexStr2Str(String hexStr) {
		String str = "0123456789ABCDEF";
		char[] hexs = hexStr.toCharArray();
		byte[] bytes = new byte[hexStr.length() / 2];
		int n;
		for (int i = 0; i < bytes.length; i++) {
			n = str.indexOf(hexs[2 * i]) * 16;
			n += str.indexOf(hexs[2 * i + 1]);
			bytes[i] = (byte) (n & 0xff);
		}
		return new String(bytes);
	}
	
}